﻿@using Microsoft.AspNetCore.Components.Forms
@using zy_simu.Data
@inject MacroService macro_service

<h5>宏处理</h5>
<div class="row" style="margin-left: 0px; font-size: 18px">
    <Textarea rows="8" @bind-Value="@macro_service.mac_value"></Textarea>
</div>

<div class="row" style="margin-left: 0px; font-size: 18px">
    <div class="col-8">
        <BootstrapInput TValue="string" @bind-Value="macro_service.mac_value"/>
    </div>
    <div class="col-4">
        <div class="btn-group" role="group">
            <Button class="btn btn-primary" OnClick="@btn_record_click" Color="@(macro_service.IsRecording ? Color.Success : Color.Primary)"> 记录 </Button>
            <Button class="btn btn-primary" OnClick="@btn_snap_click" > 快照 </Button>
            <Button class="btn btn-primary" OnClick="@btn_snap10_click"> 快照10s </Button>
            <Button class="btn btn-primary" OnClick="@btn_record_click" Color="@(macro_service.IsRecording ? Color.Success : Color.Primary)"> 存储 </Button>
            <Button class="btn btn-primary" OnClick="@btn_record_click" Color="@(macro_service.IsRecording ? Color.Success : Color.Primary)"> 加载 </Button>
        </div>
    </div>
</div>
<div class="row" style="margin-left: 0px; font-size: 18px">
    <div class="col-10">
        <BootstrapInput TValue="string" Value="@("异常点:" + macro_service.GetCheckedResults())" />
    </div>
    <div class="col-1">
        @if(result_value == "无异常")
        {
            <i class="fas fa-check" style="color:green"/>
        }
        else if(result_value.Length > 0)
        {
            <i class="fas fa-xmark" style="color:red" />
        }

    </div>
    <div class="col-1">
        <Button class="btn btn-primary" OnClick="@btn_excute_click" Color="@((macro_service.cmds_idx >= 0) ? Color.Success : Color.Primary)"> 执行 </Button>
    </div>
</div>

@code {
    private string mac_value { get; set; } = "";
    private string result_value { get; set; } = "";
    private void btn_record_click()
    {
        if (!macro_service.IsRecording)
            macro_service.StartRecording();
        else
            macro_service.StopRecording();
    }
    private void btn_snap_click()
    {
        macro_service.StartSnaping(0);
    }
    private void btn_snap10_click()
    {
        macro_service.StartSnaping(10000);
    }
    private void btn_excute_click()
    {
        macro_service.StartExcuting();
    }

    public void Refresh()
    {
        macro_service.Recording();
        macro_service.Excuting();
        string tmp = macro_service.GetCheckedResults();
        bool res = false;
        if (mac_value != macro_service.mac_value)
        {
            mac_value = macro_service.mac_value;
            res = true;
        }
        if (result_value != tmp)
        {
            result_value = tmp;
            res = true;
        }
        if (res) InvokeAsync(StateHasChanged);
    }

}
